import {Router} from '@angular/router';
import {Injectable, Injector} from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {MenuService} from "./menu.service";
import {TranslatorService} from "../translator/translator.service";
import {SettingsService} from "./settings.service";
import {ACLService} from "../acl/acl.service";

import 'rxjs/add/operator/do';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/catch';
import {_HttpClient} from "@core/services/http.client";
import {environment} from "../../../environments/environment";
/**
 * 用于应用启动时
 * 一般用来获取应用所需要的基础数据等
 */
@Injectable()
export class StartupService {
        constructor(private menuService: MenuService,
                private tr: TranslatorService,
                private settingService: SettingsService,
                private aclService: ACLService,
                private httpClient: _HttpClient,
                private injector: Injector) {
    }

    load(): Promise<any> {
        // only works with promises
        // https://github.com/angular/angular/issues/15088
        return new Promise((resolve, reject) => {
            const url = environment.SERVICE_URL + '/principal';
            this.httpClient.get(url).subscribe((data) => {
                const menus = [{text: '业务', group: true, children: data.menus}];
                this.menuService.add(menus);
                this.aclService.setAbility(data.authorities);
                this.menuService.resume();
                this.settingService.setUser(data.user);
                // resolve(data);

                this.httpClient.get('assets/app-data.json')
                    .subscribe((res: any) => {
                        this.settingService.setApp(res.app);
                        // this.settingService.setUser(res.user);
                        // 设置ＡＣＬ权限为全量
                        this.aclService.setFull(true);
                        // 初始化菜单
                        this.menuService.add(res.menu);
                        // 调整语言
                        // this.tr.use('en');

                        resolve(res);
                    }, (err: HttpErrorResponse) => {
                        resolve(null);
                    });
            }, (err: HttpErrorResponse) => {
                resolve(null);
            });
        });
    }
}
